package com.liao.jpa.dao.impl;

import com.liao.jpa.dao.CustomUser;
import com.liao.jpa.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.EntityManager;
import java.util.List;

//这个类必须叫UserDaoImpl或者CustomUserImpl
public class CustomUserImpl implements CustomUser {

    @Autowired
    private EntityManager entityManager;

    //直接用JPA的EntityManager来执行查询
    public List<TbUser> findByUsername(String username) {
        return entityManager.createQuery("select u from TbUser u where u.username=?1",TbUser.class)
                .setParameter(1,username)
                .getResultList();
    }

    //直接使用JPA的EntityManager执行原生的SQL查询
    public List<TbUser> findByUsernameBySQL(String username) {
        return entityManager.createNativeQuery("select u.* from user u where username=?1",TbUser.class)
                .setParameter(1,username)
                .getResultList();
    }
}
